package com.main.by.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.main.by.service.ValidCodeService;
import com.main.by.dao.UserDao;
import com.main.by.entity.bo.UserLoginBO;
import com.main.by.entity.vo.NewsInfoVO;
import com.main.by.entity.vo.UserRegisterVO;
import com.main.by.exception.ServiceException;
import com.main.by.service.UserService;
import com.main.by.util.MD5Util;

@Service
public class UserServiceImpl implements UserService {
	
	private static Logger logger = Logger.getLogger(UserServiceImpl.class);
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private ValidCodeService validCodeService;
	
	
	@Override
	public UserLoginBO userLogin(UserLoginBO user,HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		UserLoginBO existUser = null;
		existUser = userDao.getUserByUserName(user.getUserName());
		if(existUser == null){
			throw new ServiceException("用户不存在！");
		}
		else if(!existUser.getPassWord().equalsIgnoreCase(MD5Util.GetMD5Code(user.getPassWord()))){
			throw new ServiceException("用户密码错误！");
		}
		else if(existUser.getFlag() == 0){
			throw new ServiceException("请等待管理员审核！");
		}
		
		request.getSession().setAttribute("customUser",existUser);
		return existUser;
	}
	
	@Override
	public boolean userRegister(UserRegisterVO user,HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		if(!validCodeService.checkValidCode(user.getRandomString(), user.getType(), user.getAuthCode())){
			throw new ServiceException("验证码错误！");
		}
		UserLoginBO existUser = null;
		boolean successful = false;
		existUser = userDao.getUserByUserName(user.getUserName());
		if(existUser != null){
			if(existUser.getUserName().equals(user.getUserName())){
				throw new ServiceException("用户名已被注册！");
			}
			else{
				System.out.print(user.getUserName()+"+"+user.getPassWord());
				successful = userDao.insterRegistInfo(user);
				return successful;
			}
		}else{
			System.out.print(user.getUserName()+"+"+user.getPassWord());
			successful = userDao.insterRegistInfo(user);
			return successful;
		}
		
		
	}
	
	@Override
	public List<UserLoginBO> findAllUser(HttpServletRequest request) throws Exception {
		logger.info("查询数据库");
		return  userDao.findUser();
	}
	
	@Override
	public List<UserLoginBO> findPendingUser(HttpServletRequest request) throws Exception {
		logger.info("查询数据库");
		return  userDao.findPendingUser();
	}
	
	@Override
	public boolean auditUserInfo(int userId) throws Exception {
		logger.info("查询管理员数据库");
		return  userDao.auditUserInfo(userId);
	}
	
	@Override
	public boolean deleteUserById(int userId) throws Exception {
		logger.info("查询管理员数据库");
		return  userDao.deleteUserById(userId);
	}

}
